package lx.af.demo;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.android.volley.toolbox.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import lx.af.R;
import lx.af.base.BaseActivity;
import lx.af.manager.VolleyManager;

// TODO: not finished !!!!!!!!!!!!!!!!!!!!
/**
 * demo for com.google.volley. see detail:
 * https://android.googlesource.com/platform/frameworks/volley
 */
public class ActivityVolleyImageDemo extends BaseActivity
        implements BaseActivity.ActionBarImpl {

    private GridView mGridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_volley_image_demo);
        mGridView = getView(R.id.avid_img_grid);
        mGridView.setAdapter(new ImageAdapter(this, getData()));
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private List<String> getData() {
        ArrayList<String> list = new ArrayList<>();
        list.add("http://pic24.nipic.com/20121005/3207538_210359711140_2.jpg");
        list.add("http://pic27.nipic.com/20130316/5056611_122417762000_2.jpg");
        list.add("http://img3.uutuu.com/data3/a/ph/large/071107/2523883b0dec6b0446199080bbef32c0.jpg");
        list.add("http://pica.nipic.com/2008-03-07/200837104358118_2.jpg");
        list.add("http://pica.nipic.com/2007-08-19/2007819131018351_2.jpg");
        list.add("http://img2.imgtn.bdimg.com/it/u=3266703286,1484036492&fm=21&gp=0.jpg");
        return list;
    }

    private static class ImageAdapter extends BaseAdapter {

        Context mContext;
        List<String> mList;

        ImageAdapter(Context context, List<String> list) {
            mContext = context;
            mList = list;
        }

        @Override
        public int getCount() {
            return mList == null ? 0 : mList.size();
        }

        @Override
        public String getItem(int position) {
            return mList == null ? null : mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView img;
            if (convertView == null) {
                img = new ImageView(mContext);
            } else {
                img = (ImageView) convertView;
            }

            VolleyManager.getImageLoader().get(
                    getItem(position),
                    ImageLoader.getImageListener(img, R.drawable.ic_launcher, R.drawable.ic_launcher));

            return img;
        }
    }

}
